//核心文件
var stock={
    color:{
        border:"#ccc",//边框颜色
        withinBorder:"#dedede",//内边框颜色
        solid:"#333",//虚线颜色
        trend:"#1879fc",//K线颜色
        fill:"rgba(154,196,248,.3)",//"rgba(86,248,85,0.3)",//填充颜色
        arc:"#0453b4",	//圆心
        volume:"#999",	//成交量颜色
        red:"#dd2200",
        green:"#32a90f",
        minuteAvg:"#ffc704",//分时均线
        avg:{//日K
            _5:"#dd2200",
            _10:"#822e32",
            _20:"#f6c750",
            _30:"#155a9c"
        },
        MACD:{
            DIF:"#dd2200",
            DEA:"#155a9c"
        },
        KDJ:{
            K:"#f0f888",
            D:"#54fcfc",
            J:"#ff80ff"
        }
    },
    touch:{left:5,top:100},
    stockID:"",//当前股票ID,
    stockType:"",//股票分类
    data:null,//当前行情数据,
    isPC:false,//判断电脑还是手机
    jspath:null,//当前JS路径
    day:null,//今天日期
    current:null,//当前菜单选择
    isTime:null,


    addNum:function(num1, num2) {
        var sq1,sq2,m;
        try {sq1 = num1.toString().split(".")[1].length;}
        catch (e) {sq1 = 0;}
        try {sq2 = num2.toString().split(".")[1].length;}
        catch (e) {sq2 = 0;}
        m = Math.pow(10,Math.max(sq1, sq2));
        return (num1 * m + num2 * m) / m;
    },
    header:function(){
        var data=this.data;
        // console.log(data)
        var zuos=parseFloat(data.preClosePx)
        var dangq=parseFloat(data.tradePrice)==0?zuos:parseFloat(data.tradePrice)
        $("#astock-name").text(data.stockName)
        $("#astock-price").text(parseFloat(dangq).toFixed(2))
        $("#astock-zhangf").text((dangq>=zuos?"+":"")+parseFloat(dangq-zuos).toFixed(2))
        $("#astock-zfb").text((dangq>=zuos?"+":"")+parseFloat(((dangq-zuos)/zuos)*100).toFixed(2)+'%');

        $('#astock-time').text(data.code)
        $("#astock-high").text(parseFloat(data.highPrice).toFixed(2))

        $("#astock-low").text(parseFloat(data.lowPrice).toFixed(2));
        if(dangq>=zuos){
            $(".newBody").addClass('back-red').removeClass('back-green')
        }else{
            $(".newBody").addClass('back-green').removeClass('back-red')
        }
        $('#astock-max').text((parseFloat(zuos)+(parseFloat(zuos)*0.1)).toFixed(2))

        $('#astock-min').text((parseFloat(zuos)-(parseFloat(zuos)*0.1)).toFixed(2))

        $("#astock-open").text(parseFloat(data.openPrice).toFixed(2))
        $("#astock-volumn").text(parseFloat(data.tradeVolume/10000).toFixed(2)+"万")
        data=null;
    },
    ajax:function(data,c,o){
        var timeout=15000;
        var Action = data.Action;
        var stockID = data.stockID;
        var stockType = data.stockType;
        stockType_MY = stockType;

        if(data.Action=="current-hq"){timeout=3000;}
        var ajaxTimeoutTest=$.ajax({
            method: "get",
            //http://116.62.171.83:7080//?code=000001
            ///?code=000001
            url:Action=='minute'?api.url_stock.minute:api.url_stock.day,
            //url:  'http://qq.dy863.com'+(Action=='etf'?"/manage/etf.do":"/manage/getmarket.do"),//?Action="+Action+"&stockID="+stockID+"&stockType="+stockType
            data: data,
            dataType: "jsonp",
            jsonp: "callback",
            complete : function(XMLHttpRequest,status){
                if(status=='timeout'){
                    ajaxTimeoutTest.abort();
                }
            }
        }).done(function( data ) {
            c.call(o,true,data);
            delete data;
        }).fail(function() {
            c.call(o,true)//false true
        });
    },
    //行情
    hq:{
        time:0,
        //是否交易时间内
        index:0,
        isRun:function(){
            var date=new Date(stock.hq.time*1000);
            var t=date.getHours()+date.getMinutes()/100;
            //交易时间内 ,延迟1分钟
            if((t>=9.15&&t<11.31)||(t>=13&&t<15.01)){
                return true;
            }
            return false;
        },
        //首次加载行情
        load:function(data){

            if(data.hq!=null){
                $("#name").html(data.hq.split(",")[0]);
            }
            if(data.isHqReload){
                this.reload(function(result){
                    if(result){
                        stock.menu.load(data.default);
                        stock.menu.click();//添加点击事件
                        stock.hq.run();//启动线程
                    }else{
                        stock.error("<div onclick='window.location=window.location'>加载失败，点击重试</div>");
                    }

                });
            }else{
                stock.data=data.hq.split(",");
                //更新顶部
                stock.header();

                stock.menu.load(data.default);
                stock.menu.click();//添加点击事件
                stock.hq.run();//启动线程

            }


        },

        //更新行情
        reload:function(c,o){
            //console.log(stock.current)
            $.ajax({//详情
                type: "post",
                //async: false,
                //url: "http://114.215.237.58:1080/api/market/stocks?codes="+'000001',
                url: api.url_stock.market+'&code='+stock.stockID,
                // url: api.url_stock.market+'?Action=minute&stockID=002024&stockType=sz',
                data: {},
                dataType: "jsonp",
               jsonp: "callback",
                success:function(data){
                    // console.log(data)
                    //alert(eval("("+data+")"));
                    //console.log("-->"+data.kLineBean)
                    stock.data=data;
                    //更新顶部
                    // stock.isTime=data.flag;
                    stock.header();
                    c.call(o,true);
                }
            }).fail(function(data) {
                //alert(11);
                //console.log(data)
                c.call(o,false);
            });
        },
        run:function(){
            var _this=this;
            //启动线程
            if(!timeoutH) return false;
            _this.setTime()
             time1=setTimeout(function(){
                    _this.run();
                },3000);
            delete data;
        },
        setTime:function () {
            //stock.hq.isRun()
            if(stock.hq.isRun()){
                this.index++;
                stock.hq.reload(function(result){
                    if(result){
                        if($("#minute").length==1){
                            stock_minute.update();
                            //console.log(!$("#etf").is(":hidden"))
                        }
                        if($("#etf").length==1){
                            stock_etf.reload();
                        }
                    }
                });
                //一分钟
                //console.log(index)
                if(this.index==20){
                    this.index=0;
                    if($("#minute").length==1){
                        stock_minute.reload();
                    }
                    if($("#etf").length==1){
                        stock_etf.reload();
                    }
                }

            }else{
                // $("#dot").css({left:-1000});
            }
            stock.hq.time+=3;
        }
    },
    //错误提示
    error:function(msg,top){
        if($("#error").length==0){
            $("#stock-canvas").append('<div id="error"><div></div></div>');
        }
        $("#loading").hide();
        $("#error").show();
        if(top==undefined){top=0;}
        $("#error").css({top:top})
        $("#error div").html(msg);
    },
    configData:null,
    //只加载一次
    load:function(data){
        $.ajaxSetup({
            cache: true
        });
        $("#stock-canvas").append('<div id="loading"></div>');

        this.stockID=data.stockID;
        this.stockType=data.stockType;
        this.day=data.day;
        this.hq.time=data.time;

        this.isPC=this.script.isPC();

        if(this.isPC){
            $("#stock-canvas").css({/*width:575,*/height:325})
        }else{
            $(window).bind('resize load', function() {
                $("#stock-canvas").css("zoom", $(window).width() / 750);
            });
        }
        this.jspath=this.script.jspath();
        stock.tpl.header();//创建顶部模板

        stock.tpl.menu(data);//创建菜单
        if(data.default==undefined){
            data.default=data.menu[0].name;
        }

        this.hq.load(data);
        delete data;
    },
    //处理菜单
    menu:{

        load:function(name){
            $("#nav li").removeClass("current");
            $("#nav #nav-"+name).addClass("current");
            stock.current=name;
            $("#error").hide();
            $("#header ul").hide();
            if($("#"+name).length==0){
                $("#loading").show();
                stock.script.load(name);//引入JS
            }else{
                $("#loading").hide();
                $(".item").hide();
                $("#"+name).show();
            }


        },
        //点击
        click:function(){

            $("#nav li").click(function(){
                stock.menu.load($(this).attr("name"));
            })
        }
    },
    //模板(只创建一次)
    tpl:{
        //顶部
        header:function(){
            var str="";
            str+='<div id="net-layout"></div><div id="net-error">网络已断开</div><div id="header"><span id="name"></span><span id="current-price"></span><span id="current-bargain"></span><span id="current-time"></span><ul></ul><span id="close"></span></div>';
            $("#stock-canvas").append(str);
            //释放内存
            delete str;
        },
        //菜单
        menu:function(data){
            var str="";
            str+='<div id="nav"><ul>';
            for(var i=0;i<data.menu.length;i++){
                str+='<li name="'+data.menu[i].name+'" id="nav-'+data.menu[i].name+'"><span style="font-size: 0.26rem">'+data.menu[i].title+'</span></li>';
            }
            str+='</ul></div>';
            $("#stock-canvas").append(str);
            //释放内存
            delete str;
            delete data;
        },
        //曲线模板
        min:function(name){
            if($("#"+name).length==0){
                str="";
                str+='<div class="item item-min" id="'+name+'"><div class="touch" id="touch-'+name+'">';

                str+='<div class="K-line"><ul class="price"> <li class="red"></li> <li class="red"></li><li class="green"></li></ul><ul class="range"><li class="red"></li><li class="red"></li><li class="green"></li></ul><canvas id="'+name+'-k" class="k"></canvas></div>';
                str+='<div class="logo"></div>';

                str+='<ul class="KL-time"><li></li><li></li><li></li><li></li></ul>';

                str+='<div class="L-line" style="display:none"><ul class="volume"><li class="max-volume"></li> </ul><canvas id="'+name+'-l" class="l"></canvas></div>';

                str+='</div></div>';
                $("#stock-canvas").append(str);
                str=null;
            }

            name=null;
        },
        //K线模板
        k:function(name){
            if($("#"+name).length==0){
                str="";
                str+='<div class="item item-k" id="'+name+'"><div class="touch" id="touch-'+name+'">';

                str+='<div class="K-line"><ul class="price"> <li></li> <li></li><li></li><li></li><li></li></ul> <canvas id="'+name+'-k" class="k"></canvas></div>';
                str+='<div class="logo"></div>';

                str+='<ul class="KL-time"><li></li> <li></li><li></li><li></li><li></li></ul>';

                str+='<div class="L-line" ><ul class="volume"><li class="max-volume"></li><li></li><li></li></ul><canvas id="'+name+'-l" class="l"></canvas></div>';


                str+='<ul class="avg"></ul>';
                str+='<ul class="MACD"><li style=" color:'+stock.color.MACD.DIF+'">DIF:<span></span></li><li style="color:'+stock.color.MACD.DEA+'">DEA:<span></span></li><li style=" color:#155a9c">MACD:<span></span></li></ul>';

                str+='<ul class="KDJ"><li style=" color:'+stock.color.KDJ.K+'">K:<span></span></li><li style="color:'+stock.color.KDJ.D+'">D:<span></span></li><li style=" color:'+stock.color.KDJ.J+'">J:<span></span></li></ul>';

                str+='<div class="x"><span class="current-price"></span></div><div class="y"></div>';

                str+='</div>';

                str+='<ul id="ktool"><li class="fq" name="qfq">前复权</li><li class="current fq" name="normal">不复权</li><li class="fq" name="hfq">后复权</li><li class="current volume" name="normal">成交量</li> <li class="volume" name="MACD">MACD</li><li class="volume" name="KDJ">KDJ</li></ul>';
                str+='<div class="zoom min5_zoom"><span class="up" >+</span><span class="down" >-</span></div>';

                str+='</div>';
                $("#stock-canvas").append(str);
                str=null;
            }
            name=null;
        },
        minute:function(name){
            if($("#"+name).length==0){
                var str="";
                var sell=""
                str+='<div class="item" id="'+name+'"><div class="touch" id="touch-'+name+'">';

                str+='<div class="K-line"><div id="dot" style="display: none"></div><ul class="price"> <li></li><li></li><li></li> <li></li> <li></li></ul><ul class="range"><li></li> <li></li> <li></li> <li></li> <li></li></ul><canvas id="minute-k" class="k"></canvas></div>';

                str+='<div class="logo"></div>';

                str+='<ul class="KL-time"><li class="one">9:30</li> <li class="two">11:30/13:00</li> <li class="three">15:00</li> </ul>';

                str+='<div class="L-line"><ul class="volume"><li class="max-volume"></li> </ul><canvas id="'+name+'-l" class="l"></canvas></div>';

                str+='</div><div class="five_5"><ul id="buy"></ul><ul id="sell"></ul></div></div>';
                $("#stock-canvas").append(str);
                // $("#sell-buy").append(sell)
                str=null;
            }
            name=null;
        }
    },
    //加载JS
    script:{
        jspath:function(){
            var js="stock.js";
            var scripts = document.getElementsByTagName("script");
            var path = "";
            for (var i = 0, l = scripts.length; i < l; i++) {
                var src = scripts[i].src;
                if (src.indexOf(js) != -1) {
                    var ss = src.split(js);
                    path = ss[0];
                    break;
                }
            }
            var href = location.href;
            href = href.split("#")[0];
            href = href.split("?")[0];
            var ss = href.split("/");
            ss.length = ss.length - 1;
            href = ss.join("/");
            if (path.indexOf("https:") == -1 && path.indexOf("http:") == -1 && path.indexOf("file:") == -1 && path.indexOf("\/") != 0) {
                path = href + "/" + path;
            }
            return path;
        },
        isPC:function() {
            var userAgentInfo = navigator.userAgent;
            var Agents = ["Android", "iPhone",
                "SymbianOS", "Windows Phone",
                "iPad", "iPod"];
            var flag = true;
            for (var v = 0; v < Agents.length; v++) {
                if (userAgentInfo.indexOf(Agents[v]) > 0) {
                    flag = false;
                    break;
                }
            }
            return flag;
        },
        reload:function(name){
            $("#loading").show();
            this.load(name);
        },
        //JS入口
        load:function(name){

            this.include.load(name,function(){

                eval("stock_"+name).load(name,function(result){
                    if(!result){
                        stock.error('<span onclick="stock.script.reload(\''+name+'\')" >点击重新加载</span>',85);
                        return false;
                    }
                    $("#error").hide();
                    //创建模板
                    if(name=="minute"){
                        stock.tpl.minute(name);
                    }else if(name=="dayK"||name=="weekK"||name=="monthK"){
                        stock.tpl.k(name);
                    }else{
                        stock.tpl.min(name);
                    }
                    //画图
                    eval("stock_"+name).canvas(function(){
                        $(".item").hide();
                        $("#"+name).show();
                        $("#loading").hide();//隐藏加载框
                    });
                    delete name;
                });
            });


        },
        //加载js(只加载一次)
        include:{
            jslist:{},

            load:function(name,c,o){

                if(eval("this.jslist._"+name)==undefined){
                    eval("this.jslist._"+name+"='"+name+"'");
                    if(name=="minute"){
                        $.getScript('../js/v_cavs/stock_'+name+'.js?_time=').done(function() {
                            c.call(o);
                        }).fail(function() {
                            eval("stock.script.include.jslist._"+name+"=undefined");
                            stock.error('<span onclick="stock.script.reload(\''+name+'\')" >点击重新加载</span>',90);
                        });

                    }else{
                        $.getScript('../js/v_cavs/stock_'+name+'.js?_time=').done(function() {
                            c.call(o);
                        }).fail(function() {
                            eval("stock.script.include.jslist._"+name+"=undefined");
                            stock.error('<span onclick="stock.script.reload(\''+name+'\')" >点击重新加载</span>',90);
                        });
                    }
                }else{
                    c.call(o);
                }
            }
        }
    }
}